使用 github action 构建镜像并 push。
- git 仓库下新建
.github/workflows
文件夹,在该文件夹下创建 yml 文件,比如 docker-publish.yml 在 yml 文件中写入以下内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33name: Docker Image CI
on:
# 在 push 到 main 分支时触发
push:
branches: [ "main" ]
jobs:
build:
runs-on: ubuntu-latest
steps:
# 切换分支
- uses: actions/checkout@v4
# 登录 docker
# https://github.com/docker/login-action
- name: Docker Login
uses: docker/login-action@v3.2.0
with:
# 从仓库的 secret 获取值
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_USERNAME }}
# 执行命令构建并 push
- name: Build the Docker image
run: |
docker build --no-cache . --file Dockerfile --tag ${image_name}
docker push ${image_name}
# 使用已有的 action 构建镜像并 push
# https://github.com/docker/build-push-action
# - name: Build and push Docker image
# id: build-and-push
# uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0
# with:
# push: true
# tags: ${image_name}将该 yml 提交并 push 到 github